package org.example.rabbitmqserver02.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.*;

import java.util.Map;

@Configuration
public class RabbitmqConfigTest {
    //direct模式 点对点直连
    public static final String QUEUE_DIRECT = "queue_DIRECT";
    public static final String EXCHANGE_DIRECT = "exchange_DIRECT";
    public static final String KEY_DIRECT = "key_direct";

    @Bean(QUEUE_DIRECT)
    public Queue queue() {
        return new Queue(QUEUE_DIRECT);
    }
    @Bean(EXCHANGE_DIRECT)
    public Exchange EXCHANGE_DIRECT() {
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT).durable(true).build();
    }
    @Bean
    public Binding BINDING_QUEUE_DIRECT(@Qualifier(QUEUE_DIRECT) Queue queue,
                                        @Qualifier(EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(KEY_DIRECT).noargs();
    }


    //fanout模式 广播模式
    public static final String QUEUE_FANOUT_A = "queue_fanout_A";
    public static final String QUEUE_FANOUT_B = "queue_fanout_B";
    public static final String EXCHANGE_FANOUT = "exchange_fanout";

    @Bean(QUEUE_FANOUT_A)
    public Queue queue_fanout_A() {
        return new Queue(QUEUE_FANOUT_A);
    }
    @Bean(QUEUE_FANOUT_B)
    public Queue queue_fanout_B() {
        return new Queue(QUEUE_FANOUT_B);
    }
    @Bean(EXCHANGE_FANOUT)
    public Exchange EXCHANGE_FANOUT() {
        return ExchangeBuilder.fanoutExchange(EXCHANGE_FANOUT).durable(true).build();
    }
    @Bean
    public Binding BINDING_QUEUE_FANOUT_A(@Qualifier(QUEUE_FANOUT_A) Queue queue,
                                          @Qualifier(EXCHANGE_FANOUT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    @Bean
    public Binding BINDING_QUEUE_FANOUT_B(@Qualifier(QUEUE_FANOUT_B) Queue queue,
                                          @Qualifier(EXCHANGE_FANOUT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    //topic模式 主题模式
    public static final String QUEUE_TOPIC = "queue_topic";
    public static final String EXCHANGE_TOPIC = "exchange_topic";
    public static final String KEY_TOPIC = "topic.#";

    @Bean(QUEUE_TOPIC)
    public Queue queue_topic() {
        return new Queue(QUEUE_TOPIC);
    }
    @Bean(EXCHANGE_TOPIC)
    public Exchange EXCHANGE_TOPIC() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC).durable(true).build();
    }
    @Bean
    public Binding BINDING_QUEUE_TOPIC(@Qualifier(QUEUE_TOPIC) Queue queue,
                                       @Qualifier(EXCHANGE_TOPIC) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(KEY_TOPIC).noargs();
    }

    //headers模式
    public static final String QUEUE_HEADERS_A = "queue_headers_a";
    public static final String QUEUE_HEADERS_B = "queue_headers_b";
    public static final String EXCHANGE_HEADERS = "exchange_headers";

    @Bean(QUEUE_HEADERS_A)
    public Queue queue_headers() {
        return new Queue(QUEUE_HEADERS_A);
    }
    @Bean(QUEUE_HEADERS_B)
    public Queue queue_headers_b() {
        return new Queue(QUEUE_HEADERS_B);
    }
    @Bean(EXCHANGE_HEADERS)
    public Exchange EXCHANGE_HEADERS() {
        return ExchangeBuilder.headersExchange(EXCHANGE_HEADERS).durable(true).build();
    }

    //全部匹配handler
//    @Bean
//    public Binding BINDING_QUEUE_HEADERS(@Qualifier(QUEUE_HEADERS_A) Queue queue,
//                                         @Qualifier(EXCHANGE_HEADERS) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange)
//                .whereAll(Map.of("key1", "value1", "key2", "value2"))
//                .match();
//    }

    //部分匹配handler
//    @Bean
//    public Binding BINDING_QUEUE_HEADERS_1(@Qualifier(QUEUE_HEADERS_B) Queue queue,
//                                           @Qualifier(EXCHANGE_HEADERS) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange)
//                .whereAny(Map.of("key1", "value1", "key2", "value2"))
//                .match();
//    }
}
